package cn.mesmile.excel.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.mesmile.excel.xml.ReadXmlService;
import com.alibaba.excel.EasyExcel;
import org.apache.commons.collections4.ListUtils;
import org.dom4j.DocumentException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

/**
 * @author zb
 * @date 2021/11/17 10:26
 * @Description
 */
@RequestMapping("/api/v1/xml")
@RestController
public class XmlController {

    private final ReadXmlService readXmlService;

    public XmlController(ReadXmlService readXmlService) {
        this.readXmlService = readXmlService;
    }

    private static List<LinkedHashMap<String,Object>> result;

    @PostMapping("/read")
    public Object readXml(@RequestParam("file")MultipartFile file) throws IOException, DocumentException {
        InputStream inputStream = file.getInputStream();
        result = readXmlService.readXml(inputStream);
        return  result;
    }

    private List<List<String>> head() {
        LinkedHashMap<String, Object> map = result.get(0);
        List<List<String>> head = new ArrayList<>(map.size());
        Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> next = iterator.next();
            String key = next.getKey();
            List<String> strings = Collections.singletonList(key);
            head.add(strings);
        }
        return head;
    }

    private List<List<Object>> dataList(){
        List<List<Object>> dataList = new ArrayList<>(result.size());
        for (LinkedHashMap<String, Object> resultMap : result) {
            List<Object> objects = new ArrayList<>();
            Iterator<Map.Entry<String, Object>> iterator = resultMap.entrySet().iterator();
            while (iterator.hasNext()){
                Map.Entry<String, Object> next = iterator.next();
                Object value = next.getValue();
                objects.add(value);
            }
            dataList.add(objects);
        }
        return  dataList;
    }

    @GetMapping("/download")
    public void download(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("人员模板", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<List<String>> head = head();
        List<List<Object>> lists = dataList();
        EasyExcel.write(response.getOutputStream()).head(head).sheet("模板").doWrite(lists);
//        EasyExcel.write(response.getOutputStream(), DownloadData.class).sheet("模板").doWrite(data());
    }


}
